<template>
  <div class="login-container">
    <div class="login-box">
      <header>
        <img src="..\assets\face.png" alt />
      </header>

      <!-- <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="活动名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>-->
      <el-form
        ref="loginRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="0px"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input v-model="loginForm.username">
            <i slot="prefix" class="el-input__icon el-icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password">
            <i slot="prefix" class="iconfont icon-locking"></i>
          </el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" v-on:click="login()">登录</el-button>
          <el-button type="info" v-on:click="resetLoginForm()">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue'

export default {
  name: 'home',
  components: {
    // HelloWorld
  },
  data: function () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 31, message: '登录名称在 3 ~ 31 个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 31, message: '密码要求在 6 ~ 31 个字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      this.$refs.loginRef.resetFields()
    },
    login () {
      this.$refs.loginRef.validate(async valid => {
        if (!valid) {
          return
        }
        // console.log(valid)
        // this.$httpClient.post('login', this.loginForm)
        // 将返回结果中的data 解构到 res变量中
        const { data: res } = await this.$httpClient.post('account/login', this.loginForm)
        // console.log(res)
        if (!res.meta) {
          // console.log('网络故障')
          this.$message.error('网络故障')
        }
        if (res.meta.status !== 200) {
          // console.log('登录失败')
          this.$message.error('登录失败')
        } else {
          // console.log('登录成功')
          this.$message.success('登录成功')
          window.sessionStorage.setItem('token', res.data.token)
          this.$router.push('home')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100vh;
  position: relative;

  .login-box {
    width: 33%;
    min-width: 350px;
    height: 50%;
    background-color: #fff;

    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    border-radius: 2%;

    header {
      text-align: center;
      position: absolute;
      top: 0;
      left: 50%;
      transform: translate(-50%, -40%);

      height: 80px;
      width: 80px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 3px;
      background-color: #fff;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .login-form {
      position: absolute;
      bottom: 0;

      width: 100%;
      padding: 1em 2em;
      box-sizing: border-box;

      .btns {
        display: flex;
        justify-content: flex-end;
      }
      .el-icon-user {
        font-size: 20px;
      }
      .icon-locking {
        font-size: 24px;
      }
    }
  }
}
</style>
